
package com.sasis.dao.ibatis;

import java.util.List;

import com.sasis.dao.ibatis.BaseDaoiBATIS;
import com.sasis.model.Office;
import com.sasis.dao.OfficeDao;

import org.springframework.orm.ObjectRetrievalFailureException;

public class OfficeDaoiBatis extends BaseDaoiBATIS implements OfficeDao {

    /**
     * @see com.sasis.dao.OfficeDao#getOffices(com.sasis.model.Office)
     */
    public List getOffices(final Office office) {
          return getSqlMapClientTemplate().queryForList("getOffices", office);
    }

    /**
     * @see com.sasis.dao.OfficeDao#getOffice(Integer id)
     */
    public Office getOffice(Integer id) {
        Office office = (Office) getSqlMapClientTemplate().queryForObject("getOffice", id);

        if (office == null) {
            throw new ObjectRetrievalFailureException(Office.class, id);
        }

        return office;
    }

    /**
     * @see com.sasis.dao.OfficeDao#saveOffice(Office office)
     */    
    public void saveOffice(final Office office) {
        Integer id = office.getId();
        // check for new record
        if (id == null) {
            id = (Integer) getSqlMapClientTemplate().insert("addOffice", office);
        } else {
            getSqlMapClientTemplate().update("updateOffice", office);
        }
        if( id == null ) {
            throw new ObjectRetrievalFailureException(Office.class, id);
        }
    }

    /**
     * @see com.sasis.dao.OfficeDao#removeOffice(Integer id)
     */
    public void removeOffice(Integer id) {
        getSqlMapClientTemplate().update("deleteOffice", id);
    }
}
